//
//  LKEmoticonKeyboardView.swift
//  LKWeibo
//
//  Created by like on 16/8/18.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

class LKEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
//        backgroundColor = RandomColor()
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //添加控件
        addSubview(emoticonBottomView)
        addSubview(emoticonPageView)
        //设置约束
        emoticonBottomView.snp_makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp_top)
        }
        
        
        //底部bottom的闭包回调
        emoticonBottomView.colseure = {(type: LKEmoticonBottomViewType)->() in
            
            switch type {
            case .Default :
                print("默认")
            case .Emoji :
                print("Emoji")
            case .Lxh :
                print("浪小花")
            }
            
            //获取indexpath
            let indexpath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            //滚到到indexpath的cell
            self.emoticonPageView.scrollToItemAtIndexPath(indexpath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
    }
    //懒加载控件  底部控件
    private lazy var emoticonBottomView: LKEmoticonBottomView = LKEmoticonBottomView(frame: CGRectZero)
    
    //表情键盘
    private lazy var emoticonPageView: LKEmoticonPageView = {
        let view = LKEmoticonPageView()
        //分页
        view.pagingEnabled = true
        //弹簧
        view.bounces = false
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.delegate = self
        return view
    }()
}

//判断当前的cell的偏移量
extension LKEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let centerX = SCREENW*0.5 + scrollView.contentOffset.x
        //获取当前屏幕上显示的cells (由于系统没有帮我们排序 需要我们自己排序)
        let cells = emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        // 如果屏幕上显示两个cell 我才会判断
        if cells.count == 2 {
            // 第一个cell
            let firstCell = cells.first!
            // 最后一个cell
            let lastCell = cells.last!
            let indexPath: NSIndexPath
            // 判断中心点 是否在firstCell的frame中
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
//                printLog("first")
            }else {
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
//                printLog("last")
            }
            
            emoticonBottomView.setupCurrentButton(indexPath.section)
            
        }
    }
}




